package com.orange.res;

import java.util.HashMap;
import java.util.Map;

import com.orange.opengl.texture.TextureManager;
import com.orange.opengl.texture.atlas.texturepack.TexturePackAtlasTextureRegionFactory;
import com.orange.util.file.IFileManage;
import com.orange.util.texturepack.ITiledTexturePackTextureRegion;

public class RegionRes {

	// ===========================================================
	// 变量
	// ===========================================================
	private final IFileManage mFileManage;
	private final TextureManager mTextureManager;
	
	private final Map<String, ITiledTexturePackTextureRegion> mTiledTextureRegionMap = new HashMap<String, ITiledTexturePackTextureRegion>();

	// ===========================================================
	// 构造
	// ===========================================================
	
	public RegionRes(IFileManage pFileManage, TextureManager pTextureManager){
		this.mFileManage = pFileManage;
		this.mTextureManager = pTextureManager;
	}
	
	// ===========================================================
	// 方法
	// ===========================================================

	/**
	 * 从Assets创建纹理
	 * 
	 * @param pAssetManager
	 * @param pTextureManager
	 * @param xmlAssetPath
	 */
	public void createFromAssets(String pXmlAssetPath) {
		try {
			Map<String, ITiledTexturePackTextureRegion> tiledTextureRegionMap = TexturePackAtlasTextureRegionFactory.createTiledFromAsset(mFileManage, this.mTextureManager, pXmlAssetPath);
			if (tiledTextureRegionMap != null) {
				this.mTiledTextureRegionMap.putAll(tiledTextureRegionMap);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public ITiledTexturePackTextureRegion getTiledTextureRegion(String pTextureRegionName) {
		return this.mTiledTextureRegionMap.get(pTextureRegionName);
	}


	// ===========================================================
	// 静态方法
	// ===========================================================
	
	public static void loadTexturesFromAssets(String... pXmlAssetPaths){
		for (String xmlAssetPath : pXmlAssetPaths) {
			loadTexturesFromAssets(xmlAssetPath);
		}
	}
	
	public static void loadTexturesFromAssets(String pXmlAssetPath) {
		ResManager.getInstance().getRegionRes().createFromAssets(pXmlAssetPath);
	}
	// ===========================================================
	// 获取
	// ===========================================================
	
	public static ITiledTexturePackTextureRegion getRegion(String pTextureRegionName) {
		return ResManager.getInstance().getRegionRes().getTiledTextureRegion(pTextureRegionName);
	}
	
	/**
	 * 获取大小，返回长度为2的数组，[0]为宽，[1]为高
	 * @param pTextureRegionName
	 * @return
	 */
	public static float[] getRegionSize(String pTextureRegionName){
		float[] size = new float[2];
		ITiledTexturePackTextureRegion tiledTextureRegion = ResManager.getInstance().getRegionRes().getTiledTextureRegion(pTextureRegionName);
		size[0] = tiledTextureRegion.getWidth();
		size[1] = tiledTextureRegion.getHeight();
		return size;
	}
	
}
